package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SpffComments;
import com.ruoyi.system.service.ISpffCommentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论Controller
 * 
 * @author xjj
 * @date 2025-09-11
 */
@RestController
@RequestMapping("/system/comments")
public class SpffCommentsController extends BaseController
{
    @Autowired
    private ISpffCommentsService spffCommentsService;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpffComments spffComments)
    {
        startPage();
        List<SpffComments> list = spffCommentsService.selectSpffCommentsList(spffComments);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:comments:export')")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpffComments spffComments)
    {
        List<SpffComments> list = spffCommentsService.selectSpffCommentsList(spffComments);
        ExcelUtil<SpffComments> util = new ExcelUtil<SpffComments>(SpffComments.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:comments:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spffCommentsService.selectSpffCommentsById(id));
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:add')")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpffComments spffComments)
    {
        return toAjax(spffCommentsService.insertSpffComments(spffComments));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpffComments spffComments)
    {
        return toAjax(spffCommentsService.updateSpffComments(spffComments));
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:remove')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spffCommentsService.deleteSpffCommentsByIds(ids));
    }

    // ======== 评论管理相关接口 ========

    /**
     * 查询评论列表（包含层级结构）
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/listWithHierarchy")
    public TableDataInfo listWithHierarchy(SpffComments spffComments)
    {
        startPage();
        List<SpffComments> list = spffCommentsService.selectSpffCommentsListWithHierarchy(spffComments);
        return getDataTable(list);
    }

    /**
     * 查询评论列表（排除被屏蔽的评论）
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/listWithBlacklist")
    public TableDataInfo listWithBlacklist(SpffComments spffComments)
    {
        startPage();
        List<SpffComments> list = spffCommentsService.selectSpffCommentsListWithBlacklist(spffComments);
        return getDataTable(list);
    }

    /**
     * 屏蔽用户的所有评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/blockUser/{userId}")
    public AjaxResult blockUserComments(@PathVariable Long userId)
    {
        int result = spffCommentsService.blockUserComments(userId);
        return result > 0 ? success("用户评论已屏蔽") : error("屏蔽失败");
    }

    /**
     * 取消屏蔽用户的所有评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/unblockUser/{userId}")
    public AjaxResult unblockUserComments(@PathVariable Long userId)
    {
        int result = spffCommentsService.unblockUserComments(userId);
        return result > 0 ? success("用户评论已取消屏蔽") : error("取消屏蔽失败");
    }

    /**
     * 屏蔽单条评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/block/{commentId}")
    public AjaxResult blockComment(@PathVariable Long commentId)
    {
        int result = spffCommentsService.blockComment(commentId);
        return result > 0 ? success("评论已屏蔽") : error("屏蔽失败");
    }

    /**
     * 取消屏蔽单条评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/unblock/{commentId}")
    public AjaxResult unblockComment(@PathVariable Long commentId)
    {
        int result = spffCommentsService.unblockComment(commentId);
        return result > 0 ? success("评论已取消屏蔽") : error("取消屏蔽失败");
    }

    /**
     * 批量屏蔽评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/batchBlock")
    public AjaxResult batchBlockComments(@RequestBody Long[] commentIds)
    {
        int result = spffCommentsService.batchBlockComments(commentIds);
        return result > 0 ? success("批量屏蔽成功") : error("屏蔽失败");
    }

    /**
     * 批量启用评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/batchEnable")
    public AjaxResult batchEnableComments(@RequestBody Long[] commentIds)
    {
        int result = spffCommentsService.batchEnableComments(commentIds);
        return result > 0 ? success("批量启用成功") : error("启用失败");
    }

    /**
     * 批量删除评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:remove')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public AjaxResult batchDeleteComments(@RequestBody Long[] commentIds)
    {
        int result = spffCommentsService.batchDeleteComments(commentIds);
        return result > 0 ? success("批量删除成功") : error("删除失败");
    }

    /**
     * 根据视频ID查询评论（包含层级）
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/video/{videoId}")
    public TableDataInfo getCommentsByVideoId(@PathVariable Long videoId, SpffComments spffComments)
    {
        startPage();
        spffComments.setVideoId(videoId);
        List<SpffComments> list = spffCommentsService.selectSpffCommentsListWithHierarchy(spffComments);
        return getDataTable(list);
    }

    /**
     * 根据用户昵称搜索评论
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/searchByNickname")
    public TableDataInfo searchCommentsByNickname(SpffComments spffComments)
    {
        startPage();
        List<SpffComments> list = spffCommentsService.searchCommentsByNickname(spffComments);
        return getDataTable(list);
    }

    /**
     * 获取评论回复列表
     */
    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/replies/{parentId}")
    public TableDataInfo getCommentReplies(@PathVariable Long parentId, SpffComments spffComments)
    {
        startPage();
        spffComments.setParentId(parentId);
        List<SpffComments> list = spffCommentsService.selectSpffCommentsList(spffComments);
        return getDataTable(list);
    }
}
